package com.jinkaimen.kqb.utils;

import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Build;

import com.ht.baselibs.application.HTApplication;

import java.util.ArrayList;

/**
 * **************************************************************
 * [好刷刷]
 * **************************************************************
 * Authors: 黄涛 on 17/1/18 14:52
 * QQ：876046815
 * Email:876046815@qq.com
 */
public class PermissionUtils {


    public static boolean hasCameraPermission(){
        PackageManager pm = HTApplication.getContext().getPackageManager();
        return (PackageManager.PERMISSION_GRANTED == pm.checkPermission(Manifest.permission.CAMERA, HTApplication.getContext().getPackageName()));
    }

    public static boolean hasWriteSDCardPermission(){
        PackageManager pm = HTApplication.getContext().getPackageManager();
        return (PackageManager.PERMISSION_GRANTED == pm.checkPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, HTApplication.getContext().getPackageName()));
    }
    /**
     * 权限校验
     * @param activity
     */
    public static void checkPermission(Activity activity, String ... permissions) {
        if (Build.VERSION.SDK_INT >= 23) {
            int requestCode = 0;
            ArrayList<String> permissionsInfo = new ArrayList<String>();

            for (String item :
                    permissions) {
                int check = activity.checkSelfPermission(item);

                if(check != PackageManager.PERMISSION_GRANTED){
                    requestCode |= 1 << 0;
                    permissionsInfo.add(item);
                }

            }
            if (requestCode > 0) {
                String[] permission = new String[permissionsInfo.size()];
                activity.requestPermissions(permissionsInfo.toArray(permission), requestCode);
            }
        }
    }
}
